package com.example.yuejian23.yuejian_23.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian23.yuejian_23.Adapter.PeixunAdapter;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.PeixunBean;
import com.example.yuejian23.yuejian_23.service.PeixunService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SousuoActivity extends AppCompatActivity {
    String keyword;
    TextView name_title;
    EditText sousuo;
    ImageView peixun_thumb,ss;
    RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sousuo);

        intview();
        ss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keyword = sousuo.getText().toString();
                if(keyword == ""){
                    Toast.makeText(SousuoActivity.this,"小伙子不想练练肌肉么",Toast.LENGTH_SHORT).show();
                }else {
                    getsousuodetil();
                }
            }
        });


    }

    public void getsousuodetil() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<List<PeixunBean>> call = retrofit.create(PeixunService.class).sou(keyword);
        call.enqueue(new Callback<List<PeixunBean>>() {
            @Override
            public void onResponse(Call<List<PeixunBean>> call, Response<List<PeixunBean>> response) {
                List<PeixunBean> mysou = response.body();

                recyclerView.setAdapter(new PeixunAdapter(SousuoActivity.this,mysou));
            }

            @Override
            public void onFailure(Call<List<PeixunBean>> call, Throwable t) {

            }
        });

    }


    private void intview() {
        name_title = (TextView) findViewById(R.id.name_title);
        sousuo = (EditText) findViewById(R.id.sousuo);
        peixun_thumb = (ImageView) findViewById(R.id.peixun_thumb);
        ss = (ImageView) findViewById(R.id.ss);
        recyclerView = (RecyclerView) findViewById(R.id.sou);
        recyclerView.setLayoutManager(new LinearLayoutManager(SousuoActivity.this));
    }
}
